Custom backend eni keewis#1
Conversation
* Copy 2025 tutorial to 2026 as template * update TOC * Update workshop page for 2026
…owale/xarray-tutorial into custom-backend-eni-keewis
keewis
left a comment
There was a problem hiding this comment.
feel free to take over my branch (or merge the branch and create a separate PR on the main repo) if that makes the workflow easier
| time_dim: str, | ||
| plot: bool = False, | ||
| plot_outpath: str ='plot_image', | ||
| ): |
There was a problem hiding this comment.
we usually call these to_*, so may be a good option? Otherwise save_dataset (for symmetry with open_dataset?
| if plot: | ||
| img_count = len(dataset[time_dim]) | ||
| jpg_names = [] | ||
| variable_da = dataset[variable] | ||
| for i in range(img_count): | ||
| plt.figure() | ||
| variable_da[i].plot( | ||
| x="lon", | ||
| y="lat", | ||
| vmin=variable_da.min(), | ||
| vmax=variable_da.max()) | ||
| jpg_name = f'img_data/{plot_outpath}_{i}.jpg' | ||
| plt.savefig(jpg_name) | ||
| jpg_names.append(jpg_name) | ||
| self.write_jpg_gif(jpg_names, out_filename) |
There was a problem hiding this comment.
I would argue that this should be an accessor method instead. Completely changing behavior of a function based on a flag feels really weird to me
Edit: but maybe this is just a different way of writing the data? In which case I'd use a method="matplotlib" or method="imageio" parameter.
There was a problem hiding this comment.
yeah, i was thinking of it as more so a different way of writing the data since the source, an xr.Dataset, is the same. i like that method="matplotlib" is more explicit so i will go with that.
|
Closing for PR to upstream. |
| plt.savefig(jpg_name) | ||
| jpg_names.append(jpg_name) | ||
| self.write_jpg_gif(jpg_names, out_filename) |
There was a problem hiding this comment.
you could use animations instead of writing to image files, then aggregating into gif:
https://matplotlib.org/stable/users/explain/animations/animations.html#animation-writers
| frames_array = np.stack( | ||
| [dataset[variable][x] for x in range(len(dataset[time_dim]))], axis=0 | ||
| ) | ||
| iio.imwrite( | ||
| out_filename, | ||
| frames_array, | ||
| extension=".gif", | ||
| loop=0, | ||
| duration=100, | ||
| background=1, | ||
| ) |
There was a problem hiding this comment.
I guess that's about as far as you can get with gifs since the color palette is something it forces onto you. Local color tables should help quite a bit, but in the end you'll need a way to (and also invert that)
- normalize the data
- discretize (if you pass
local_color_tabletoimwriteand provided "local" means "block-local" not "frame-local", it might be possible to skip this) - look up colors
The last step should be pretty easy to invert (and the second can probably be skipped for block-local color tables), but for the first we need the normalization parameters, which may be tricky: pillow does not appear to support writing application extension blocks. As a workaround, we can create a {gif-path}.json (e.g. test.gif.json) to store the metadata (if it doesn't exist we skip the inversion of the normalization step).
The above would get us pretty close to what the matplotlib method is doing, which also follows the normalization + colormap lookup / interpolation steps (though there may be additional elements like axis ticks or labels).
Building off of @keewis branch!
I was thinking about actual science workflows for manipulating gifs and writing time series plot images as gifs came to mind.
Here is a quick little example:
cc
@VeckoTheGecko and @ianhi